﻿using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.BussinessVoucher;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Voucher;

namespace DataObjects.ADO.SqlServer.Voucher
{
    /// <summary>
    /// 
    /// </summary>
    public class SqlServerFixedAssetArmortizationDao : IFixedAssetArmortizationDao
    {

        public FixedAssetArmortization GetFixedAssetArmortization(long itemTransactionId)
        {
            const string sql = @"uspGet_FixedAssetArmortization_ByID";
            object[] parms = { "@RefID", itemTransactionId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<FixedAssetArmortization> GetPaged(long unitId, long refTypeId, int currentPage)
        {
            const string sql = @"uspGetPaged_FixedAssetArmortization";
            object[] parms = { "@WhereClause", null, "@OrderBy", null, "@PageIndex", currentPage - 1, "@UnitID", unitId };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public int GetPageSize(long unitId, long refTypeId)
        {
            const string sql = @"uspGetPageSize_FixedAssetArmortization";
            object[] parms = { "@WhereClause", null, "@UnitID", unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

        string IFixedAssetArmortizationDao.InsertFixedAssetArmortization(FixedAssetArmortization itemTransaction)
        {
            const string sql = "uspInsert_FixedAssetArmortization";
            return Db.Insert(sql, CommandType.StoredProcedure, Take(itemTransaction));
        }


        public string UpdateFixedAssetArmortization(FixedAssetArmortization itemTransaction)
        {
            const string sql = "uspUpdate_FixedAssetArmortization";
            return Db.Update(sql, CommandType.StoredProcedure, Take(itemTransaction));
        }

        public string DeleteFixedAssetArmortization(FixedAssetArmortization itemTransaction)
        {
            const string sql = @"uspDelete_FixedAssetArmortization";
            object[] parms = { "@FixedAssetArmortizationID", itemTransaction.RefId };
            return Db.Delete(sql, CommandType.StoredProcedure, parms);
        }

        public long GetSequence()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// The make
        /// </summary>
        private static readonly Func<IDataReader, FixedAssetArmortization> Make = reader =>
            new FixedAssetArmortization
            {
                RefId = reader["RefID"].AsLong(),
                RefTypeId = reader["RefTypeID"].AsInt(),
                RefNo = reader["RefNo"].AsString(),
                RefDate = reader["RefDate"].AsDateTime(),
                TotalAmount = reader["TotalAmountOC"].AsDecimal(),
                PostedDate = reader["PostedDate"].AsDateTime(),
                JournalMemo = reader["JournalMemo"].AsString(),
                UnitId = reader["UnitID"].AsLong()
            };

        /// <summary>
        /// Takes the specified itemTransaction.
        /// </summary>
        /// <returns></returns>
        private object[] Take(FixedAssetArmortization fAArmortization)
        {
            return new object[]  
            {
                @"@RefID", fAArmortization.RefId,
                @"RefTypeID", fAArmortization.RefTypeId,
                @"RefNo", fAArmortization.RefNo,
                @"RefDate", fAArmortization.RefDate,
                @"PostedDate", fAArmortization.PostedDate,
                @"TotalAmount", fAArmortization.TotalAmount,
                @"JournalMemo", fAArmortization.JournalMemo,
                @"UnitID", fAArmortization.UnitId
            };
        }
    }
}
